#!/bin/bash

# API 信息
IP="172.29.20.32"
PORT="11005"
MODEL="DeepSeek-R1-Distill-70B-IN8"

# 请求体
REQUEST_BODY='{
  "model": "DeepSeek-R1-Distill-70B-IN8",
  "messages": [
    {"role": "user", "content": "你好?"},
    {"role": "assistant", "content": "请介绍重庆好玩的地方？"}
  ],
  "max_tokens": 1150,
  "temperature": 0.7,
  "top_p": 1.0
}'

# 临时文件
TIME_LOG="/tmp/api_call_times.log"

# 清空旧的记录文件
> "$TIME_LOG"

# 调用函数
call_api() {
  local start_time=$(date +%s%3N)
  curl -s -X POST "http://$IP:$PORT/v1/chat/completions" \
    -H "Content-Type: application/json" \
    -d "$REQUEST_BODY" \
    --write-out "%{time_total}\n" \
    --output /dev/null > /tmp/time_output.txt

  local end_time=$(date +%s%3N)
  local duration=$((end_time - start_time))
  echo "调用 $i: 耗时 $duration ms"
  echo "$duration" >> "$TIME_LOG"
}

# 主循环调用
for ((i=1; i<=8; i++)); do
  call_api
  if [ $i -lt 8 ]; then
    sleep 3
  fi
done

# 输出统计信息
if [ -f "$TIME_LOG" ]; then
  echo -e "\n=== 调用耗时统计 ==="
  min_time=$(sort -n "$TIME_LOG" | head -n1)
  max_time=$(sort -n "$TIME_LOG" | tail -n1)
  avg_time=$(awk '{sum += $1; count++} END {printf "%.2f", sum/count}' "$TIME_LOG")

  echo "最小耗时: $min_time ms"
  echo "最大耗时: $max_time ms"
  echo "平均耗时: $avg_time ms"
else
  echo "未记录到调用耗时数据。"
fi

# 清理临时文件
rm -f /tmp/time_output.txt
